// 立项选择展示隐藏效果
$('#remember').change(function () {
    var status = $('#remember').prop("checked");
    if (status) {
        $('.hideprobox').show();
        $(".tarhideprobox").show();
        $(".hideprobox").show();
    } else {
        $('.hideprobox').hide();
        $(".tarhideprobox").hide();
        $(".hideprobox").hide();
    }
});

function ckIsN(value) {
    if (value == "N") {
        $("#pdLine").find("input[type='checkbox']").each(function (i, n) {
            if ($(n).val() != "N") {
                $(n).attr("checked", false);
            }
        });
    } else {
        $("#pdLine").find("input[type='checkbox']").each(function (i, n) {
            if ($(n).val() == "N") {
                $(n).attr("checked", false);
            }
        });
    }
}

function findProJbxx(CertusFormAjax, $routeParams, $scope) {
    CertusFormAjax.ajax("findComList", {}, function (response) {
        $scope.data = response.data;
        findStageInfo(CertusFormAjax, $routeParams, $scope);
    });

    CertusFormAjax.ajax("getPdlines", {}, function (response) {
        $scope.dd = response.data;
    });

    CertusFormAjax.ajax("findViewCompany", {}, function (response) {
        var data = response.data;
        if (data.indexOf("STSJT") > -1) {
            $scope.isStsjt = true;
        }
        if (data.indexOf("STSGD") > -1) {
            $scope.isGdxx = true;
        }
        if (data.indexOf("HFKJ") > -1) {
            $scope.isHfkj = true;
        }
        if (data.indexOf("isGdxx") > -1) {
            $scope.isStsjt = true;
        }
    });


    $(".submit").on('click', function () {
        if (!certusValidateNotnull()) {
            return;
        }
        CertusFormAjax.ajax("isHasProName", { "proName": $("#proName").val() }, function (res) {
            if (res.data == true) {
                layer.msg("项目名称已存在！", { "time": 1000 });
            } else {
                var sTime = $("#datetime-5").val();
                var endTime = $("#datetime-6").val();
                if (endTime == "") {
                    layer.msg("结束日期不能为空", { time: 1000 });
                    return;
                }
                if (sTime != "") {
                    var startTime = new Date(sTime.replace("/", "-").replace("/", "-"));
                    var endTimes = new Date(endTime.replace("/", "-").replace("/", "-"));
                    if (startTime > endTimes) {
                        layer.msg("开始日期不能大于结束日期", { time: 1000 });
                        return;
                    }
                } else {
                    layer.msg("开始日期不能为空", { time: 1000 });
                    return;
                }

                //客户交际费用
                var ctStartTime = $("#ctStartTime").val();
                var ctEndTime = $("#ctEndTime").val();
                if (new Date(ctStartTime.replace("/", "-").replace("/", "-"))
                    > new Date(ctEndTime.replace("/", "-").replace("/", "-"))) {
                    layer.msg("客户交际费用开始时间不能大于结束时间", { time: 1000 });
                    return;
                }

                //差旅费用
                var bsStartTime = $("#bsStartTime").val();
                var bsEndTime = $("#bsEndTime").val();
                if (new Date(bsStartTime.replace("/", "-").replace("/", "-"))
                    > new Date(bsEndTime.replace("/", "-").replace("/", "-"))) {
                    layer.msg("差旅费用开始时间不能大于结束时间", { time: 1000 });
                    return;
                }

                //采购费用
                var phStartTime = $("#phStartTime").val();
                var phEndTime = $("#phEndTime").val();
                if (new Date(phStartTime.replace("/", "-").replace("/", "-"))
                    > new Date(phEndTime.replace("/", "-").replace("/", "-"))) {
                    layer.msg("采购费用开始时间不能大于结束时间", { time: 1000 });
                    return;
                }

                var pdLineCodes = "";
                if (!$("#pdLine").is(":hidden")) {
                    $("#pdLine").find("input[type='checkbox']:checked").each(function (i, n) {
                        if (i == 0) {
                            pdLineCodes += $(n).val();
                        } else {
                            pdLineCodes += "," + $(n).val();
                        }
                    });
                    if (pdLineCodes == "") {
                        layer.msg("至少选择一个产品线", { time: 1000 });
                        return;
                    }
                }
                var arr = $("form").serializeArray();
                var data = {};
                for (var i = 0; i < arr.length; i++) {
                    data[arr[i].name] = arr[i].value;
                };
                var isCheck = document.getElementById("remember").checked;
                data.isCheck = isCheck;
                data.warnData = warnData;
                data.otherMoneyData = otherMoneyData;
                data.proType = $scope.proType;
                data.targetDate = targetDate;
                data.startTime = $("#datetime-5").val();
                data.endTime = $("#datetime-6").val();
                data.pdLineCodes2 = pdLineCodes;
                data.isMg = $('#remember').prop("checked");
                data.lxLevel = $("#lxLevel").val();
                data.urgency = $("#urgency").val();
                data.importance = $("#importance").val();
                data.rlData = rlData;
                data.wjData = wjData;
                // 开启
                var index = layer.load(1);
                CertusFormAjax.ajax("toLxSq", { "data": JSON.stringify(data) }, function (response) {
                    //关闭
                    layer.close(index);
                    if (response.data == "true") {
                        layer.msg("申请成功");
                        window.history.back();
                    }
                });
            }
        });
    });
}

//添加人力成本
function hideRl() {
    $(".hiderl").hide();
    cleanRl();
}

function hideWj() {
    $(".wjhider").hide();
}

function cleanWj() {
    $("#wjName").val("");
    $("#wjComments").val("");
}

$(".hideprobox").hide();

function findStageInfo(CertusFormAjax, $routeParams, $scope) {
    CertusFormAjax.ajax("findProStageInfo", { "proId": $routeParams.proId }, function (response) {
        var data = response.data;
        $scope.proArea = data.proArea;
        $scope.proType = data.proType;
        if (data.proType == "XS") {
            $scope.showCus = true;
            $scope.cusId = data.cusId + "";
            $scope.showArea = true;
            $scope.showSign = true;
            $scope.showHk = true;
            $scope.showPolucy = true;
            $scope.showCompe = true;
            $scope.showMoney = true;
            $scope.showBudget = true;
            $scope.showCtMoney = true;
            $scope.showCompare = true;

            $("#datetime-5").prop("disabled", "");
            $("#datetime-5").val("");
            $("#datetime-6").prop("disabled", "");
            $("#datetime-6").val("");
        } else if (data.proType == "ZL" || data.proType == "SW"
            || data.proType == "BM") {
            if (data.proType == "BM") {
                var date = new Date;
                var year = date.getFullYear();
                $("#datetime-5").prop("disabled", "disabled");
                $("#datetime-5").val(year + "-01-01");
                $("#datetime-6").prop("disabled", "disabled");
                $("#datetime-6").val(year + "-12-31");
            }
            $scope.showCus = false;
            $scope.showArea = false;
            $scope.showCompare = false;
            $scope.showSign = false;
            $scope.showHk = false;
            $scope.showPolucy = false;
            $scope.showCompe = false;
            $scope.showMoney = false;
            $scope.showBudget = false;
            $scope.showCtMoney = true;
            $scope.showFinish = true;
            $scope.showVersion = false;
            $("#lxLevel").val(data.proLv);
            $("#lxLevel").prop("disabled", "disabled");
            $("#urgency").val(data.urgency);
            $("#urgency").prop("disabled", "disabled");
            $("#importance").val(data.importance);
            $("#importance").prop("disabled", "disabled");
        } else {
            $scope.showCus = false;
            $scope.showArea = false;
            $scope.showCompare = false;
            $scope.showSign = false;
            $scope.showHk = false;
            $scope.showPolucy = false;
            $scope.showCompe = false;
            $scope.showMoney = false;
            $scope.showBudget = false;
            $scope.showVersion = true;
            $scope.showFinish = false;
            $("#datetime-5").prop("disabled", "");
            $("#datetime-5").val("");
            $("#datetime-6").prop("disabled", "");
            $("#datetime-6").val("");
        }
        $scope.proName = data.proName;
        $scope.proId = $routeParams.proId;

        CertusFormAjax.ajax("getNlxRoles",{"company":data.compId},function (response) {
            var data = response.data;
            $scope.rlList = data;
        });

        CertusFormAjax.ajax("getProFiles",{},function (response) {
            var data = response.data;
            $scope.wjList = data;
        });
    });
    this.showRl = function () {
        $(".hiderl").show();
    }
    this.showWj = function () {
        $(".wjhider").show();
    };
};

var rlColumns = [{
    id: 'roleName',
    title: '角色',
    type: 'string',
    columnClass: 'text-center'
},{
    id: 'rtMoney',
    title: '级别',
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        if (value=="1"){
            content = "低";
        }else if (value=="2"){
            content = "中";
        }else{
            content = "高";
        }
        return content;
    }
},{
    id: 'beginTime',
    title: '开始时间',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'endTime',
    title: '结束时间',
    type: 'string',
    columnClass: 'text-center',
},{
    id: 'rtNum',
    title: '人天',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '说明',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteRl(' + record.rid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var rlData = [];
var rlOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '人力成本列表',
    datas: rlData,
    columns: rlColumns,
    gridContainer: 'rlContainer',
    toolbarContainer: 'rlToolBarContainer',
    tools: ''
};
var rlGrid = $.fn.dlshouwen.grid.init(rlOption);
rlGrid.load();

var rid = 1;
$(".rlSubmit").click(function () {
    var rArr = {};
    var userName = $('#rluserName option:selected').val();
    var userId = $("#rluserName").val();
    if (userId==""){
        layer.msg("请选择角色！");
        return;
    }
    var rtMoney = $("#rtMoney").val();
    var rlBeginTime = $("#rlBeginTime").val();
    var rlEndTime = $("#rlEndTime").val();
    if (rlBeginTime==""||rlEndTime==""){
        layer.msg("请输入使用周期！");
        return;
    }
    var rtNum = daysBetween(rlBeginTime,rlEndTime);
    var rlComments = $("#rlComments").val();
    rArr.roleName = userName;
    rArr.roleId = userId;
    rArr.rtMoney = rtMoney;
    rArr.beginTime = rlBeginTime;
    rArr.endTime = rlEndTime;
    rArr.comments = rlComments;
    rArr.rtNum = rtNum;
    rArr.rid = rid;
    //判断成员是否已经存在
    for(var i=0;i<rlData.length;i++){
        if (rlData[i].roleId==userId){
            layer.msg("成员已经存在！");
            return;
        }
    }
    rid++;
    rlData.push(rArr);
    rlGrid.load();
    $(".hiderl").hide();
    cleanRl()
});

//删除人力
function deleteRl(rid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < rlData.length; i++) {
            if (rlData[i].rid == rid) {
                dArr = rlData[i];
            }
        }
        rlData.splice($.inArray(dArr, rlData), 1);
        rlData.load();
        layer.msg('删除成功');
    });
}
function cleanRl() {
    $("#rluserName").val("");
    $("#rlBeginTime").val("");
    $("#rlEndTime").val("");
    $("#rlComments").val();
}
//人天预估 结束

//涉及文件
var wjColumns = [{
    id: 'wjName',
    title: '文件名称',
    type: 'string',
    columnClass: 'text-center'
}, {
    id: 'comments',
    title: '说明',
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWj(' + record.pwid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var wjData = [];
var wjOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '涉及文件列表',
    datas: wjData,
    columns: wjColumns,
    gridContainer: 'wjContainer',
    toolbarContainer: 'wjToolBarContainer',
    tools: ''
};
var wjGrid = $.fn.dlshouwen.grid.init(wjOption);
wjGrid.load();
var pwid = 1;
$(".wjSubmit").click(function () {
    // var wjId = $("#wjName").val();
    // if (wjId==""){
    //     layer.msg("请选择文件！");
    //     return;
    // }
    // var wjName = $('#wjName option:selected').val();
    var wjName = $("#wjName").val();
    var comments = $("#wjComments").val();
    var arr = {};
    arr.pwid = pwid;
    // arr.wjId = wjId;
    arr.wjName = wjName;
    arr.comments = comments;
    // for(var i=0;i<wjData.length;i++){
    //     if (wjData[i].wjId==wjId){
    //         layer.msg("文件已经存在！");
    //         return;
    //     }
    // }
    pwid++;

    wjData.push(arr);
    wjGrid.load();
    $(".wjhider").hide();
    cleanWj()
});

function deleteWj(pwid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < wjData.length; i++) {
            if (wjData[i].pwid == pwid) {
                dArr = wjData[i];
            }
        }
        wjData.splice($.inArray(dArr, wjData), 1);
        wjGrid.load();
        layer.msg('删除成功');
    });
}

function daysBetween(sDate1,sDate2){
    //Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
    var time1 = Date.parse(new Date(sDate1));
    var time2 = Date.parse(new Date(sDate2));
    var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
    return  nDays;
};

function showtarget() {
    $("#proTarget").val("");
    $("#tarFinishTime").val("");
    $(".tarhideproin").show();
};
tarhideproin();
function tarhideproin() {
    $(".tarhideproin").hide();
};


function showproin() {
    $("#wntyName").val("");
    $("#wntyDesc").val("");
    $("#finishTime").val("");
    $(".hideproin").show();
};
//项目指标表单显示隐藏
function hideproin() {
    $(".hideproin").hide();
};
// 立项信息-项目目标
var prointColumns = [{
    id: 'proTarget',
    title: '项目目标',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'finishTime',
    title: '完成时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    type: 'string',
    width: 75,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteTarget(' + record.wid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];

var targetDate = [];
//项目目标列表
var prointOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '项目目标',
    datas: targetDate,
    columns: prointColumns,
    gridContainer: 'targetContainer',
    tools: ''
};
var prointGrid = $.fn.dlshouwen.grid.init(prointOption);
prointGrid.load();
var tarid = 1;
$(".targetSubmit").on('click', function () {
    var proTarget = $("#proTarget").val();
    var finishTime = $("#tarFinishTime").val();
    if (proTarget == "") {
        layer.msg("项目目标不能为空");
        return;
    }
    if (proTarget.length > 120) {
        layer.msg("项目目标不能大于120字");
        return;
    }
    if (finishTime == "") {
        layer.msg("完成时间不能为空");
        return;
    }
    var targetArr = {};
    targetArr.proTarget = proTarget;
    targetArr.finishTime = finishTime;
    targetArr.wid = tarid;
    targetDate.push(targetArr);
    tarid++;
    prointGrid.load();
    $(".tarhideproin").hide();
});
function deleteTarget(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < targetDate.length; i++) {
            if (targetDate[i].wid == wid) {
                dArr = targetDate[i];
            }
        }
        targetDate.splice($.inArray(dArr, targetDate), 1);
        prointGrid.load();
        layer.msg('删除成功');
    });
}

// 立项信息-项目指标
var proinColumns = [{
    id: 'wntyName',
    title: '指标名称',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'wntyDesc',
    title: '指标描述',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'finishTime',
    title: '完成时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWarnA(' + record.wid + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var warnData = [];
//项目指标列表
var proinOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '项目指标',
    datas: warnData,
    columns: proinColumns,
    gridContainer: 'proinContainer',
    toolbarContainer: 'proinToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var proinGrid = $.fn.dlshouwen.grid.init(proinOption);
proinGrid.load();
var warnI = 1;
$(".warnSubmit").on('click', function () {
    var wntyName = $("#wntyName").val();
    if (wntyName == "" || wntyName == null || wntyName == undefined) {
        layer.msg("指标名称不能为空！")
        return;
    }
    var wntyDesc = $("#wntyDesc").val();
    if (wntyDesc == "" || wntyDesc == null || wntyDesc == undefined) {
        layer.msg("指标描述不能为空！")
        return;
    }
    var finishTime = $("#finishTime").val();
    if (finishTime == "" || finishTime == null || finishTime == undefined) {
        layer.msg("完成时间不能为空！")
        return;
    }
    var warnArr = {};
    warnArr.wntyName = wntyName;
    warnArr.wntyDesc = wntyDesc;
    warnArr.finishTime = finishTime;
    warnArr.wid = warnI;
    warnData.push(warnArr);
    warnI++;
    proinGrid.load();
    $(".hideproin").hide();
});

function deleteWarnA(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < warnData.length; i++) {
            if (warnData[i].wid == wid) {
                dArr = warnData[i];
            }
        }
        warnData.splice($.inArray(dArr, warnData), 1);
        proinGrid.load();
        layer.msg('删除成功');
    });
}
//项目其他费用表单显示隐藏
function hideother() {
    $(".hideother").hide();
};

function showother() {
    $(".hideother").show();
};

var otherColumns = [{
    id: 'comments',
    title: '事由',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'bugetMoney',
    title: '预算金额',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
      }
}, {
    id: 'budgetBegin',
    title: '开始时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'budgetEnd',
    title: '结束时间',
    type: 'string',
    width: 100,
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record) {
        var content = '';
        content += '<button class="btn btn-xs btn-danger" onclick="deleteWarn(' + record.id + ');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];
var otherMoneyData = [];
var otherOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '其他费用',
    datas: otherMoneyData,
    columns: otherColumns,
    gridContainer: 'otherContainer',
    toolbarContainer: 'otherToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var otherGrid = $.fn.dlshouwen.grid.init(otherOption);
otherGrid.load();

var om = 1;
function addOtherMoney() {
    var bugetMoney = $("#bugetMoney").val();
    if (bugetMoney == "" || bugetMoney == undefined || bugetMoney == null) {
        layer.msg("预算金额不能为空!");
        return;
    }
    var comments = $("#comments").val();
    if (comments == "" || comments == undefined || comments == null) {
        layer.msg("事由不能为空!");
        return;
    }
    var arr = {};
    arr.id = om;
    arr.moneyType = $("#moneyType").val();
    arr.bugetMoney = bugetMoney;
    arr.comments = comments;
    var budgetBegin = $("#oStartTime").val();
    var budgetEnd = $("#oEndTime").val();
    if (budgetBegin == null || budgetBegin == "" || budgetBegin == undefined) {
        layer.msg('开始日期不能为空!');
        return;
    }
    if (budgetEnd == null || budgetEnd == "" || budgetEnd == undefined) {
        layer.msg('结束日期不能为空!');
        return;
    }
    if (new Date(budgetBegin.replace("/", "-").replace("/", "-")) >
        new Date(budgetEnd.replace("/", "-").replace("/", "-"))) {
        layer.msg('开始日期不能大于结束日期!');
        return;
    }
    arr.budgetBegin = budgetBegin;
    arr.budgetEnd = budgetEnd;
    otherMoneyData.push(arr);
    om++;
    otherGrid.load();
    $(".hideother").hide();

    $("#bugetMoney").val("");
    $("#oStartTime").val("");
    $("#oEndTime").val("");
    $("#comments").val("");
}
function deleteWarn(wid) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        var dArr = {};
        for (var i = 0; i < otherMoneyData.length; i++) {
            if (otherMoneyData[i].id == wid) {
                dArr = otherMoneyData[i];
            }
        }
        otherMoneyData.splice($.inArray(dArr, otherMoneyData), 1);
        otherGrid.load();
        layer.msg('删除成功');
    });
}